/*****************************************************************************/
/**
 *  @file   OverViewUpdator.cpp
 *  @author Naohisa Sakamoto
 */
/*----------------------------------------------------------------------------
 *
 *  Copyright (c) Visualization Laboratory, Kyoto University.
 *  All rights reserved.
 *  See http://www.viz.media.kyoto-u.ac.jp/kvs/copyright/ for details.
 *
 *  $Id$
 */
/*****************************************************************************/
#include "OverViewUpdator.h"
#include "OverView.h"
#include "DataSet.h"
#include "Frustum.h"
#include <kvs/Vector3>
#include <kvs/Xform>
#include <kvs/RGBColor>
#include <kvs/ObjectManager>
#include <kvs/ObjectBase>
#include <kvs/LineObject>


OverViewUpdator::OverViewUpdator( OverView* over_view )
{
    m_over_view = over_view;
}

void OverViewUpdator::attachDataSet( DataSet* data_set )
{
    m_data_set = data_set;
}

void OverViewUpdator::update()
{
    Frustum frustum( this->scene() );

    kvs::Xform xform = this->scene()->objectManager()->object()->xform();
    const kvs::Vec3 t = xform.translation();
    const kvs::Vec3 s = xform.scaling();
    xform = kvs::Xform::Translation( -t ) * xform;
    xform = kvs::Xform::Scaling( kvs::Vec3::All( 1.0f ) / s ) * xform;
    for ( size_t i = 1; i <= m_data_set->bounds().ranges.size() + 1; i++ )
    {
        m_over_view->scene()->objectManager()->object(i)->setXform( xform );
    }

    for ( size_t i = 0; i < m_data_set->bounds().ranges.size(); i++ )
    {
        const size_t index = i + 2;
        kvs::ObjectBase* o = m_over_view->scene()->objectManager()->object( index );
        kvs::LineObject* b = kvs::LineObject::DownCast( o );
        b->setColor( kvs::RGBColor::Red() );
        b->show();

        const kvs::Vec3& min = m_data_set->bounds().ranges[i].min;
        const kvs::Vec3& max = m_data_set->bounds().ranges[i].max;
        if ( !frustum.include( min, max ) ) { b->hide(); }
    }

    m_over_view->redraw();
}
